package logic

import (
	"demo/models"
	db "demo/util/mysql"
	"fmt"
)

type orderLogic struct{}

var OrderLogic orderLogic

// 创建订单
func (this orderLogic) Create(param models.Order) error {
	param.Id = 0
	_, err := db.Engine.Insert(param)
	if err != nil {
		fmt.Printf("logic module create err:%v", err)
		return err
	}
	return nil
}

// 更新订单
func (this orderLogic) Update(param models.Order) error {
	_, err := db.Engine.Update(param)
	if err != nil {
		fmt.Printf("logic update err:%v", err)
		return err
	}
	return nil
}

// 删除订单
func (this orderLogic) Remove(param models.Order) error {
	param.Status = 0
	_, err := db.Engine.Update(param)
	if err != nil {
		fmt.Printf("logic remove err%v", err)
		return err
	}
	return nil
}

// 订单列表
func (this orderLogic) List() ([]models.Order, error) {
	var result = make([]models.Order, 0)
	err := db.Engine.Find(&result)
	if err != nil {
		fmt.Printf("logic list err%v", err)
		return []models.Order{}, nil
	}
	return result, nil
}

// 订单详情
func (this orderLogic) Detail(id int) (models.Order, error) {
	var result models.Order
	_, err := db.Engine.ID(id).Get(&result)
	if err != nil {
		fmt.Printf("logic detail err%v", err)
		return models.Order{}, err
	}
	return result, nil
}
